package com.uniriho.lxr5002_key_switch_lh

import android.annotation.SuppressLint
import android.content.Context
import android.opengl.Visibility
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.view.animation.BounceInterpolator
import android.view.animation.TranslateAnimation
import androidx.compose.runtime.currentRecomposeScope
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.ViewModel
import com.sdk.devicemanager.ICcon
import com.uniriho.lxr5002_key_switch_lh.bean.CountInfo
import com.uniriho.lxr5002_key_switch_lh.databinding.ActivityMainBinding
import com.uniriho.lxr5002_key_switch_lh.utils.ShareUtils
import com.uniriho.lxr5002_key_switch_lh.view.BadgeView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@SuppressLint("StaticFieldLeak")
class MainMoudel: ViewModel() {

    private lateinit var bvLeftCount: BadgeView
    private lateinit var bvRightCount: BadgeView
    private lateinit var bvLeftXingCount: BadgeView
    private lateinit var bvRightXingCount: BadgeView
    private lateinit var bvBackCount: BadgeView
    private lateinit var bvMoreCount: BadgeView
    private lateinit var bvEnterCount: BadgeView
    private lateinit var bvJiaCount: BadgeView
    private lateinit var bvJianCount: BadgeView
    private lateinit var bvEditCount: BadgeView
    private lateinit var bvKey1Count: BadgeView
    private lateinit var bvKey2Count: BadgeView
    private lateinit var bvKey3Count: BadgeView
    private lateinit var bvKey4Count: BadgeView
    private lateinit var bvKey5Count: BadgeView
    private lateinit var bvKey6Count: BadgeView
    private lateinit var bvKey7Count: BadgeView
    private lateinit var bvKey8Count: BadgeView
    private lateinit var bvKey9Count: BadgeView
    private lateinit var bvKey0Count: BadgeView
    private lateinit var bvSwitchOpenCount: BadgeView
    private lateinit var bvSwitchCloseCount: BadgeView
    private lateinit var bvKeyF1Count: BadgeView
    private lateinit var bvKeyF2Count: BadgeView
    private lateinit var mContext: Context

    private lateinit var countInfo:CountInfo

    //数字动画
    private val anim by lazy {
        TranslateAnimation(-100f, 0f, 0f, 0f)
    }

    /**
     * 保存按键计数
     */
    fun saveKeyCount(context: Context){
        ShareUtils.putCountInfo(countInfo,context)
    }


    private lateinit var mainDataBinding:ActivityMainBinding
    /**
     * 获取计数
     */
    fun getKeyCount(context: Context): CountInfo? {
        return ShareUtils.getCountInfo(context)
    }

    fun disPatchEvent(event: KeyEvent) {
        Log.e("keyCode","${event.keyCode}")
        if (event.action == KeyEvent.ACTION_UP) {
            changeBG_Value(event.keyCode,android.graphics.Color.RED)
        }
        if (event.action == KeyEvent.ACTION_DOWN) {
            changeBg(event.keyCode,android.graphics.Color.BLUE)
        }
    }

    /**
     * 根据key值，改变对应控件背景色
     *
     * @param keyCode
     * @param color
     */
    @SuppressLint("ResourceAsColor")
    private fun changeBG_Value(keyCode:Int,color: Int) {
        when (keyCode) {
            140 -> {
                bvLeftCount.setText("${++countInfo.bvLeftCount}")
                bvLeftCount.setBackgroundColor(color)
            }

            141 -> {
                bvRightCount.setText("${++countInfo.bvRightCount}")
                bvRightCount.setBackgroundColor(color)
            }

            139 -> {
                bvLeftXingCount.setText("${++countInfo.bvLeftXingCount}")
                bvLeftXingCount.setBackgroundColor(color)
            }

            138 -> {
                bvRightXingCount.setText("${++countInfo.bvRightXingCount}")
                bvRightXingCount.setBackgroundColor(color)
            }

            4 -> {
                bvBackCount.setText("${++countInfo.bvBackCount}")
                bvBackCount.setBackgroundColor(color)
            }

            133 -> {
                bvMoreCount.setText("${++countInfo.bvMoreCount}")
                bvMoreCount.setBackgroundColor(color)
            }

            66 -> {
                bvEnterCount.setText("${++countInfo.bvEnterCount}")
                bvEnterCount.setBackgroundColor(color)
            }

            81 -> {
                bvJiaCount.setText("${++countInfo.bvJiaCount}")
                bvJiaCount.setBackgroundColor(color)
            }

            69 -> {
                bvJianCount.setText("${++countInfo.bvJianCount}")
                bvJianCount.setBackgroundColor(color)
            }

            134 -> {
                bvEditCount.setText("${++countInfo.bvEditCount}")
                bvEditCount.setBackgroundColor(color)
            }

            8 -> {
                bvKey1Count.setText("${++countInfo.bvKey1Count}")
                bvKey1Count.setBackgroundColor(color)
            }

            9 -> {
                bvKey2Count.setText("${++countInfo.bvKey2Count}")
                bvKey2Count.setBackgroundColor(color)
            }

            10 -> {
                bvKey3Count.setText("${++countInfo.bvKey3Count}")
                bvKey3Count.setBackgroundColor(color)
            }

            11 -> {
                bvKey4Count.setText("${++countInfo.bvKey4Count}")
                bvKey4Count.setBackgroundColor(color)
            }

            12 -> {
                bvKey5Count.setText("${++countInfo.bvKey5Count}")
                bvKey5Count.setBackgroundColor(color)
            }

            13 -> {
                bvKey6Count.setText("${++countInfo.bvKey6Count}")
                bvKey6Count.setBackgroundColor(color)
            }

            14 -> {
                bvKey7Count.setText("${++countInfo.bvKey7Count}")
                bvKey7Count.setBackgroundColor(color)
            }

            15 -> {
                bvKey8Count.setText("${++countInfo.bvKey8Count}")
                bvKey8Count.setBackgroundColor(color)
            }

            16 -> {
                bvKey9Count.setText("${++countInfo.bvKey9Count}")
                bvKey9Count.setBackgroundColor(color)
            }

            7 -> {
                bvKey0Count.setText("${++countInfo.bvKey0Count}")
                bvKey0Count.setBackgroundColor(color)
            }

            131 -> {
                bvKeyF1Count.setText("${++countInfo.bvKeyF1Count}")
                bvKeyF1Count.setBackgroundColor(color)
            }

            132 -> {
                bvKeyF2Count.setText("${++countInfo.bvKeyF2Count}")
                bvKeyF2Count.setBackgroundColor(color)
            }

        }

    }
    @SuppressLint("ResourceAsColor")
    private fun changeBg(keyCode:Int, color: Int) {
        when (keyCode) {
            140 -> {
                bvLeftCount.setBackgroundColor(color)
            }

            141 -> {
                bvRightCount.setBackgroundColor(color)
            }

            139 -> {
                bvLeftXingCount.setBackgroundColor(color)
            }

            138 -> {
                bvRightXingCount.setBackgroundColor(color)
            }

            4 -> {
                bvBackCount.setBackgroundColor(color)
            }

            133 -> {
                bvMoreCount.setBackgroundColor(color)
            }

            66 -> {
                bvEnterCount.setBackgroundColor(color)
            }

            81 -> {
                bvJiaCount.setBackgroundColor(color)
            }

            69 -> {
                bvJianCount.setBackgroundColor(color)
            }

            134 -> {
                bvEditCount.setBackgroundColor(color)
            }

            8 -> {
                bvKey1Count.setBackgroundColor(color)
            }

            9 -> {
                bvKey2Count.setBackgroundColor(color)
            }

            10 -> {
                bvKey3Count.setBackgroundColor(color)
            }

            11 -> {
                bvKey4Count.setBackgroundColor(color)
            }

            12 -> {
                bvKey5Count.setBackgroundColor(color)
            }

            13 -> {
                bvKey6Count.setBackgroundColor(color)
            }

            14 -> {
                bvKey7Count.setBackgroundColor(color)
            }

            15 -> {
                bvKey8Count.setBackgroundColor(color)
            }

            16 -> {
                bvKey9Count.setBackgroundColor(color)
            }

            7 -> {
                bvKey0Count.setBackgroundColor(color)
            }

            131 -> {
                bvKeyF1Count.setBackgroundColor(color)
            }

            132 -> {
                bvKeyF2Count.setBackgroundColor(color)
            }

        }

    }

    /**
     * 初始化数据
     */
    @SuppressLint("SuspiciousIndentation")
    fun init(binding: ActivityMainBinding, context: Context) {
        ICcon.getInstance().debugMode(true)
        mContext = context
        mainDataBinding = binding
            anim.setInterpolator(BounceInterpolator())
            anim.duration = 100

            //初始化历史按键次数
            val keyCount = getKeyCount(mContext)
            if (keyCount != null) {
                countInfo =keyCount
            }else{
                countInfo = CountInfo()
            }
            //初始化数字显示控件
            initViewKeyCountAnim()
            //监听拨动开关
            switchListener()

    }

    /**
     * 监听波动开关
     */
    private var currentSwitchState: Int = 0
    fun switchListener() {
        // 4 -> {bvSwitchCount     .setText("${countInfo.bvSwitchCount   ++}")}
        currentSwitchState = ICcon.getInstance()._switchStatus
        if (10 == currentSwitchState) {
            bvSwitchOpenCount.setBackgroundColor(android.graphics.Color.RED)
            bvSwitchCloseCount.setBackgroundColor(android.graphics.Color.BLUE)
        }else{
            bvSwitchOpenCount.setBackgroundColor(android.graphics.Color.BLUE)
            bvSwitchCloseCount.setBackgroundColor(android.graphics.Color.RED)
        }
        CoroutineScope(Dispatchers.IO).launch {
            while (true) {
                val switchStatus = ICcon.getInstance()._switchStatus
                if (currentSwitchState != switchStatus) {
                    currentSwitchState = switchStatus
                    if (11 == currentSwitchState) {
                        bvSwitchOpenCount.text = "${++countInfo.bvSwitchOpenCount}"
                        bvSwitchOpenCount.setBackgroundColor(android.graphics.Color.BLUE)
                        bvSwitchCloseCount.setBackgroundColor(android.graphics.Color.RED)
                    }else{
                        bvSwitchCloseCount.text = "${++countInfo.bvSwitchCloseCount}"
                        bvSwitchCloseCount.setBackgroundColor(android.graphics.Color.BLUE)
                        bvSwitchOpenCount.setBackgroundColor(android.graphics.Color.RED)
                    }
                }
                delay(100)
            }
        }
    }


    /**
     * 初始化数字控件
     */
    fun initViewKeyCountAnim() {
        bvLeftCount = getBadgeV(mainDataBinding.tvKeyLeft, countInfo.bvLeftCount.toString())
        bvRightCount = getBadgeV(mainDataBinding.tvKeyRight, countInfo.bvRightCount.toString())
        bvLeftXingCount =
            getBadgeV(mainDataBinding.tvKeyScan1, countInfo.bvLeftXingCount.toString())
        bvRightXingCount =
            getBadgeV(mainDataBinding.tvKeyScan2, countInfo.bvRightXingCount.toString())
        bvBackCount = getBadgeV(mainDataBinding.tvKeyBack, countInfo.bvBackCount.toString())
        bvMoreCount = getBadgeV(mainDataBinding.tvKeyMore, countInfo.bvMoreCount.toString())
        bvEnterCount = getBadgeV(mainDataBinding.tvKeyEnter, countInfo.bvEnterCount.toString())
        bvJiaCount = getBadgeV(mainDataBinding.tvKeyJia, countInfo.bvJiaCount.toString())
        bvJianCount = getBadgeV(mainDataBinding.tvKeyJian, countInfo.bvJianCount.toString())
        bvEditCount = getBadgeV(mainDataBinding.tvKeyEdit, countInfo.bvEditCount.toString())
        bvKey1Count = getBadgeV(mainDataBinding.tvKey1, countInfo.bvKey1Count.toString())
        bvKey2Count = getBadgeV(mainDataBinding.tvKey2, countInfo.bvKey2Count.toString())
        bvKey3Count = getBadgeV(mainDataBinding.tvKey3, countInfo.bvKey3Count.toString())
        bvKey4Count = getBadgeV(mainDataBinding.tvKey4, countInfo.bvKey4Count.toString())
        bvKey5Count = getBadgeV(mainDataBinding.tvKey5, countInfo.bvKey5Count.toString())
        bvKey6Count = getBadgeV(mainDataBinding.tvKey6, countInfo.bvKey6Count.toString())
        bvKey7Count = getBadgeV(mainDataBinding.tvKey7, countInfo.bvKey7Count.toString())
        bvKey8Count = getBadgeV(mainDataBinding.tvKey8, countInfo.bvKey8Count.toString())
        bvKey9Count = getBadgeV(mainDataBinding.tvKey9, countInfo.bvKey9Count.toString())
        bvKey0Count = getBadgeV(mainDataBinding.tvKey0, countInfo.bvKey0Count.toString())
        bvSwitchOpenCount = getBadgeV(mainDataBinding.textSwtichOpen, countInfo.bvSwitchOpenCount.toString())
        bvSwitchCloseCount = getBadgeV(mainDataBinding.textSwtichClse, countInfo.bvSwitchCloseCount.toString())
        bvKeyF1Count = getBadgeV(mainDataBinding.tvKeyF1, countInfo.bvKeyF1Count.toString())
        bvKeyF2Count = getBadgeV(mainDataBinding.tvKeyF2, countInfo.bvKeyF2Count.toString())
        bvLeftCount.show()
        bvRightCount.show()
        bvLeftXingCount.show()
        bvRightXingCount.show()
        bvBackCount.show()
        bvMoreCount.show()
        bvEnterCount.show()
        bvJiaCount.show()
        bvJianCount.show()
        bvEditCount.show()
        bvKey1Count.show()
        bvKey2Count.show()
        bvKey3Count.show()
        bvKey4Count.show()
        bvKey5Count.show()
        bvKey6Count.show()
        bvKey7Count.show()
        bvKey8Count.show()
        bvKey9Count.show()
        bvKey0Count.show()
        bvSwitchOpenCount.show()
        bvSwitchCloseCount.show()
        bvKeyF1Count.show()
        bvKeyF2Count.show()
    }

    /**
     * 获取数字view
     *
     * @param v
     * @param str
     * @return
     */
    private fun getBadgeV(v: View, str: String): BadgeView {
        val mBadgeView = BadgeView(mContext, v)
        mBadgeView.setText(str)
        mBadgeView.setWidth(100)
        mBadgeView.setHeight(60)
        mBadgeView.setBadgeMargin(0, 0)
        return mBadgeView
    }

}